package com.cq.androidtools.jetpack.navigation.user

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.cq.androidtools.R
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
 * A simple [Fragment] subclass.
 * Use the [FragmentHome.newInstance] factory method to
 * create an instance of this fragment.
 */
class FragmentHome : PrintFragment() {
    private lateinit var viewPager2 : ViewPager2
    private lateinit var demoCollectionAdapter : DemoCollectionAdapter

    override fun onCreateView(
        inflater : LayoutInflater, container : ViewGroup?,
        savedInstanceState : Bundle?,
    ) : View? {
        return inflater.inflate(R.layout.fragment_home, container, false)
    }

    override fun onViewCreated(view : View, savedInstanceState : Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewPager2 = view.findViewById(R.id.pager)
        demoCollectionAdapter = DemoCollectionAdapter(this)
        viewPager2.adapter = demoCollectionAdapter
        val tabLayout = view.findViewById<TabLayout>(R.id.tab_layout)
        TabLayoutMediator(tabLayout, viewPager2, true, false) { tab, pos ->
            tab.text = "OBJECT ${(pos + 1)}"
        }.attach()
    }

}

class DemoCollectionAdapter(fragment : Fragment) : FragmentStateAdapter(fragment) {
    override fun getItemCount() = 5
    override fun createFragment(position : Int) : Fragment {
        val fragment = DemoObjectFragment()
        fragment.arguments = Bundle().apply {
            putInt(ARG_OBJECT, position + 1)
        }
        return fragment
    }
}

private const val ARG_OBJECT = "object"

class DemoObjectFragment1 : Fragment() {

    private var page = 0

    override fun onCreateView(inflater : LayoutInflater, container : ViewGroup?, savedInstanceState : Bundle?) : View? {
        return inflater.inflate(R.layout.fragment_collection_object1, container, false)
    }

    override fun onViewCreated(view : View, savedInstanceState : Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        page = arguments?.getInt(ARG_OBJECT)!!
        arguments?.takeIf { it.containsKey(ARG_OBJECT) }?.apply {
            val textView : TextView = view.findViewById(R.id.text1)
            Log.e("------onViewCreated", "page = $page")
            textView.text = getInt(ARG_OBJECT).toString()
        }
    }

    override fun onResume() {
        super.onResume()
        Log.e("------onResume", "page = $page")
    }

    override fun onDestroyView() {
        super.onDestroyView()
        Log.e("------onDestroyView", "page = $page")
    }
}

class DemoObjectFragment : Fragment() {

    private var page = 0

    override fun onCreateView(inflater : LayoutInflater, container : ViewGroup?, savedInstanceState : Bundle?) : View? {
        return inflater.inflate(R.layout.fragment_collection_object, container, false)
    }

    override fun onViewCreated(view : View, savedInstanceState : Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        page = arguments?.getInt(ARG_OBJECT)!!
        arguments?.takeIf { it.containsKey(ARG_OBJECT) }?.apply {
            val textView : TextView = view.findViewById(R.id.text1)
            Log.e("------onViewCreated", "page = $page")
            textView.text = getInt(ARG_OBJECT).toString()
        }
    }

    override fun onResume() {
        super.onResume()
        Log.e("------onResume", "page = $page")
    }

    override fun onDestroyView() {
        super.onDestroyView()
        Log.e("------onDestroyView", "page = $page")
    }
}